import { defineStore } from 'pinia'
import { ref } from 'vue'
import { setToken as cookieSetToken, removeToken as cookieRemoveToken } from '@/utils/cookie'
// 用户模块
export const useUserStore = defineStore(
  'zlcode-one-oj-user',
  () => {
    const token = ref('') // 定义 token
    const setToken = (t) => {
      token.value = t // 设置 token
      cookieSetToken(t) // 同时设置到 cookie 中
    }

    const removeToken = () => {
      token.value = ''
      cookieRemoveToken() // 同时从 cookie 中移除
    }

    const userInfo = ref({})

    const setUserInfo = (data) => {
      userInfo.value = data
    }

    const exit = () => {
      token.value = ''
      userInfo.value = {}
    }

    return {
      token,
      setToken,
      removeToken,
      userInfo,
      setUserInfo,
      exit
    }
  },
  {
    persist: true // 持久化
  }
)
